﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Xml.Serialization;

namespace ScreenUp
{
    static class Program
    {
        public static int X, Y;
        public static Size size;
        public static Form1 f1;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Dictionary<string, string> validationResults = NasuTek.Licensing.Automated.Validation.Validate("SG", "ScreenGrab");
            if(validationResults["ValidationResult"] == "true")
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                f1 = new Form1();
                if (validationResults["AlreadyActivated"] == "false")
                {
                    Form1.df.ASID = validationResults["ASIDKey"];
                    Form1.df.ActivationSerialKey = validationResults["Serial"];
                    SerializeDataFile(Form1.df);
                }
                //InterceptKeys.Hook();
                Application.Run(f1);
            }
            else
            {
                MessageBox.Show("A NasuTek ScreenGrab License is missing or failed to verify on the NLS Server. For more information on activation see http://nasutek.com/activation/?o=help.", "NasuTek ScreenGrab", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        static public void SerializeDataFile(DataFile d)
        {
            // Serialization
            XmlSerializer s = new XmlSerializer(typeof(DataFile));
            TextWriter w = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ScreenGrab.plist"));
            s.Serialize(w, d);
            w.Close();
        }

        static public DataFile DeserializeDataFile()
        {
            try
            {
                // Deserialization
                DataFile newList;
                XmlSerializer s = new XmlSerializer(typeof(DataFile));
                TextReader r = new StreamReader(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ScreenGrab.plist"));
                newList = (DataFile)s.Deserialize(r);
                r.Close();
                return newList;
            }
            catch
            {
                return new DataFile();
            }
        }
    }
}
